library(data.table)
library(ggplot2)
library(lfe)
library(stringr)
library(scales)
library(Hmisc)
library(stargazer)


scale <- function(x) {
  
  y <- (x - mean(x,na.rm=T))/sd(x,na.rm=T)
  
}



hmda.in <- fread('../data/loans07to17conventional.csv') # Hmda data---conventional loans from 2007-2017 data
bd <- fread('../data/bankdata_conventional.csv')        # Bank capitalization data

with.avery <- merge(hmda.in,bd[,-c('bank'), with = F],by.x=c('rssdid','year'),by.y=c('rssdid','year'))
with.avery[,lender_state_year := paste(rssdid,state_code,year)]



#standardization
with.avery[,z_cr_gap := scale(cr_gap)]
with.avery[,z_log_income := scale(log(income.y))]
with.avery[,z_log_loan_amount := scale(log(loan_amount.y))]
with.avery[,z_log_originations := scale(log(total_originations))]
with.avery[,z_log_unique_cts := scale(log(unique_cts))]
with.avery[,z_log_assets := scale(log(total_assets))]
with.avery[,z_deposit_ratio := scale(deposit_ratio)]
with.avery[,z_non_core := scale(noncore_funding)]
with.avery[,z_core := scale(core_deposits)]
with.avery[,z_cr := scale(cr)]


#Table 4
t4.1 <- felm(held ~ z_cr_gap + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | tract_year | 0 | lender_state_year, data = with.avery)
t4.2 <- felm(held ~ z_cr_gap + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | rssdid + tract_year | 0 | lender_state_year, data = with.avery)
t4.3 <- felm(jumbo ~ z_cr_gap + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | tract_year | 0 | lender_state_year, data = with.avery)
t4.4 <- felm(jumbo ~ z_cr_gap + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | rssdid + tract_year | 0 | lender_state_year, data = with.avery)
t4.5 <- felm(held ~ z_cr_gap + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | tract_year | 0 | lender_state_year, data = with.avery, weights = with.avery$conforming)
t4.6 <- felm(held ~ z_cr_gap + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | rssdid + tract_year | 0 | lender_state_year, data = with.avery, weights = with.avery$conforming)

stargazer(t4.1,t4.2,t4.3,t4.4,t4.5,t4.6,type = 'html')

#Appendix B2 C
a2.c.1 <- felm(held ~ z_cr + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | tract_year | 0 | lender_state_year, data = with.avery)
a2.c.2 <- felm(held ~ z_cr + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | rssdid + tract_year | 0 | lender_state_year, data = with.avery)
a2.c.3 <- felm(jumbo ~ z_cr + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | tract_year | 0 | lender_state_year, data = with.avery)
a2.c.4 <- felm(jumbo ~ z_cr + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | rssdid + tract_year | 0 | lender_state_year, data = with.avery)
a2.c.5 <- felm(held ~ z_cr + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | tract_year | 0 | lender_state_year, data = with.avery, weights = with.avery$conforming)
a2.c.6 <- felm(held ~ z_cr + z_log_income + z_log_loan_amount + z_log_originations + z_log_unique_cts + z_log_assets + z_deposit_ratio + z_non_core + z_core | rssdid + tract_year | 0 | lender_state_year, data = with.avery, weights = with.avery$conforming)

stargazer(a2.c.1,a2.c.2,a2.c.3,a2.c.4,a2.c.5,a2.c.6,type = 'html')

